package Demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Cards {
    //4个花色  13张牌
    public static final String[] suits={"♥","♠","♣","♦"};
    //生成一副牌52张  4*13 不包含大小王
    public List<Card> buyCard(){
        List<Card> cardList=new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                int rank=j;
                String suit=suits[i];
                Card card=new Card(rank,suit);
                cardList.add(card);//生成一张牌
            }
        }
        return cardList;
    }
    //洗牌
    public void shuffle(List<Card> cardList){
        Random random=new Random();
        //让i=51，生成一个0~i的随机数，与i交换。i--，重复此过程，洗牌
        for (int i = cardList.size()-1;i>0; i--) {
            int randIndex= random.nextInt(i);//生成0~i的随机数放入randIndex
            swap(cardList,i,randIndex);
        }
    }
    //写一个交换方法，交换i和j下标的牌
    private void swap(List<Card> cardList,int i,int j){
        Card tmp=cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);
    }

    //三个人， 每个人轮流抓5张牌
    public void drawCard(List<Card> cardList){
        List<Card> hand1=new ArrayList<>();
        List<Card> hand2=new ArrayList<>();
        List<Card> hand3=new ArrayList<>();

        List<List<Card>>hands=new ArrayList<>();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);

        for (int i = 0; i < 5; i++) {//i代表5张牌
            for (int j = 0; j < 3; j++) {//j代表3个人
                Card card=cardList.remove(0);    //怎么抓牌？ 每次删除0下标的牌
                hands.get(j).add(card);//hands.get(0)=hand1,以此类推

            }
        }
        System.out.println("第1个人的牌："+hand1);
        System.out.println("第2个人的牌："+hand2);
        System.out.println("第3个人的牌："+hand3);
    }
}
